package de.koller.worlders.engine.entity.plattform;

import de.koller.oceanFramework.OceanException;
import de.koller.oceanFramework.math.Vec;
import de.koller.worlders.WorldersRoot;
import de.koller.worlders.engine.entity.IRenderable;
import de.koller.worlders.engine.entity.ITileEntity;
import de.koller.worlders.engine.entity.mo.MovingObject;
import de.koller.worlders.engine.tiles.ITile;
import de.koller.worlders.engine.tiles.Tile;
import de.koller.worlders.engine.util.WLightWeightNode;
import java.util.ArrayList;
import java.util.List;

public class Plattform extends WLightWeightNode implements ITileEntity, ITile, Comparable<Plattform> {

	private Tile tile;
	private final List<IPlattformEffect> effects = new ArrayList<>();
	private boolean inits = false;
	private int effectPriority = Integer.MIN_VALUE;
	
	public Plattform() {

	}

	@Override
	public int compareTo( Plattform o ) {
		return -Integer.compare( effectPriority, o.effectPriority );
	}

	public void setPriority( int priority ) {
		if( this.effectPriority > priority )
			return;
		this.effectPriority = priority;
	}

	public int getEffectPriority() {
		return effectPriority;
	}
	
	public Plattform addEffect( IPlattformEffect effect ) {
		effects.add( effect );
		if( inits )
			effect.init( this );
		return this;
	}

	@Override
	public void generate( WorldersRoot root, Object... references ) {
		super.generate( root, references );
		
		if( references.length < 1 || references[0] instanceof Tile == false )
			throw new OceanException( "first argument must be the TileObject!" );
		tile = (Tile)references[0];
			
		for( IPlattformEffect e : effects )
			e.init( this );
		
		inits = true;
	}

	@Override
	public void ungenerate() {
		super.ungenerate();
		
		for( IPlattformEffect e : effects )
			e.release();
	}
	
	public void collision( Vec wayBefore, Vec wayAfter, MovingObject mo ) {
		for( IPlattformEffect e : effects )
			e.collision( wayBefore, wayAfter, mo );
	}
	
	public boolean isAbove( MovingObject mo ) {
		return mo.getPosition().getY() + mo.getSize().getY() < this.getPosition().getY();
	}
	
	public boolean isBelow( MovingObject mo ) {
		return mo.getPosition().getY() > this.getPosition().getY() + this.getSize().getY();
	}
	
	public boolean isRight( MovingObject mo ) {
		return mo.getPosition().getX() > this.getPosition().getX() + this.getSize().getX();
	}
	
	public boolean isLeft( MovingObject mo ) {
		return mo.getPosition().getX() + mo.getSize().getX() < this.getPosition().getX();
	}

	@Override
	public Tile getTile() {
		return tile;
	}

	@Override
	public Vec getPosition() {
		return tile.getPosition();
	}

	@Override
	public Vec getSize() {
		return tile.getSize();
	}

	public List<IPlattformEffect> getEffects() {
		return effects;
	}

	@Override
	public IRenderable getVisual() {
		return tile.getVisual();
	}

	@Override
	public float getZ() {
		throw new UnsupportedOperationException( "Not supported yet." );
	}
	
}
